``
function cleanMarkdown(markdownString) {
// Remove fenced code blocks but keep the content
let cleaned = markdownString.replace(/```[a-zA-Z]*\n?([\s\S]*?)```/g, '$1')
// Remove indented code blocks but keep the content (remove 4 spaces or 1 tab from start of line)
cleaned = cleaned.replace(/^( |\t)(.*)$/gm, '$2')
// Remove inline code blocks but keep the content
cleaned = cleaned.replace(/`([^`\n]*)`/g, '$1')
// Remove HTML tags but keep their content
cleaned = cleaned.replace(/
]*>/g, '')
// Normalize nested blockquotes to single level
// This handles cases like "> > > text" or "> > text" and converts to "> text"
cleaned = cleaned.replace(/^(>\s*)+/gm, '> ')
// Clean up excessive whitespace that may result from removals
cleaned = cleaned.replace(/\n\s*\n\s*\n/g, '\n\n') // Replace multiple empty lines with double newline
cleaned = cleaned.trim() // Remove leading/trailing whitespace
return cleaned
}
// The type of the event payload is extracted
const { type } = outgoingEvent.payload
// Log the payload if a 'custom' type of payload is used for future references
if (type == 'custom') {
console.warn('Custom event sent', outgoingEvent.payload)
}
if (outgoingEvent.preview) {
outgoingEvent.preview = cleanMarkdown(outgoingEvent.preview)
}
if (outgoingEvent.payload.text) {
outgoingEvent.payload.text = cleanMarkdown(outgoingEvent.payload.text)
}
``